package com.zzb.servlet.newstype;


import com.alibaba.fastjson.JSONObject;
import com.zzb.DTO.ResultDTO;
import com.zzb.VO.NewsTypeVO;
import com.zzb.service.NewsTypeService;
import com.zzb.service.impl.NewsTypeServiceImpl;
import com.zzb.util.Tool;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/back/news/type/update")
public class NewsTypeUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //乱码处理
        req.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        resp.setCharacterEncoding(StandardCharsets.UTF_8.toString());

        //得到前端数据
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String enable = req.getParameter("enable");
        String isShow = req.getParameter("isShow");
        String remark = req.getParameter("remark");
        String title = req.getParameter("title");



        //对前端数据进行封装
        NewsTypeVO newsTypeVO = new NewsTypeVO();
        newsTypeVO.setId(Integer.valueOf(id));
        newsTypeVO.setIsShow(Integer.valueOf(Tool.nullToNum(isShow)));
        newsTypeVO.setName(Tool.nullToStr(name));
        newsTypeVO.setEnable(Integer.valueOf(Tool.nullToNum(enable)));
        newsTypeVO.setRemark(Tool.nullToStr(remark));
        newsTypeVO.setTitle(Tool.nullToStr(title));

        //调用服务逻辑
        NewsTypeService newsTypeService = new NewsTypeServiceImpl();

        //得到返回值
        ResultDTO resultDTO = newsTypeService.update(newsTypeVO);
        //将数据响应给前端
        resp.getWriter().println(JSONObject.toJSONString(resultDTO));
    }
}
